home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / SNDLIST- / UTILITIE.C < prev   
Text File  |  1988-11-10  |  5KB  |  234 lines

  1. /*
  2.     Sound lister
  3.     "Utilities.c"
  4. */
  5.  
  6. #include <DialogMgr.h>
  7. #include <ResourceMgr.h>
  8. #include <ToolboxUtil.h>
  9. #include "SndList.h"
  10.  
  11. /* ----- Get resource id of sound --------------------------------------- */
  12.  
  13. short Extract(name)
  14. register unsigned char *name;
  15. {
  16.     register short  i, j;
  17.     register unsigned char c;
  18.     unsigned char num[7];
  19.     long id;
  20.  
  21.     i = j = 0;
  22.     while ((c = name[j++]) == 0xCA)            /* Skip spaces */
  23.         if (j > sizeof(num)-1)
  24.             return 0x8000;
  25.     do {                                        /* Copy id */
  26.         num[++i] = c;
  27.         if (i > sizeof(num))
  28.             return 0x8000;
  29.     } while ((c = name[j++]) != 0xCA && c);
  30.     num[0] = i;
  31.     StringToNum(num, &id);
  32.     return (short)id;
  33. }
  34.  
  35. /* ----- Opne/close resource file --------------------------------------- */
  36.  
  37. short OpenResources(name, volume, current)
  38. unsigned char *name;            /* File name */
  39. short volume;                    /* Volume reference number */
  40. short *current;                /* Old reference number */
  41. {
  42.     register short  ref;
  43.  
  44.     *current = CurResFile();    /* Save current resource file reference */
  45.     if (SetVol(0L, volume))
  46.         return -1;                /* Error */
  47.     SetResLoad(FALSE);            /* Don't load all the resources now */
  48.     ref = OpenResFile(name);
  49.     SetResLoad(TRUE);
  50.     return ref;                    /* Is -1 if there was an error */
  51. }
  52.  
  53. void CloseResources(old, ref)
  54. short old;                        /* Old resource file reference */
  55. short ref;                        /* The one to be closed */
  56. {
  57.     /* Don't close appl/sys resource file */
  58.     if (ref != -1 && ref != old && ref != SysMap)
  59.         CloseResFile(ref);
  60.     UseResFile(old);
  61. }
  62.  
  63. /* ----- Outline button in dialog --------------------------------------- */
  64.  
  65. void OutLine(dialog, item, mode)
  66. register DialogPtr dialog;
  67. register short item;
  68. register short mode;
  69. {
  70.     short kind;
  71.     Handle h;
  72.     Rect r;
  73.     PenState state;
  74.  
  75.     SetPort(dialog);
  76.     GetPenState(&state);
  77.     GetDItem(dialog, item, &kind, &h, &r);
  78.     PenMode(mode);
  79.     PenPat(&black);
  80.     PenSize(3, 3);
  81.     InsetRect(&r, -4, -4);
  82.     FrameRoundRect(&r, 16, 16);
  83.     SetPenState(&state);
  84. }
  85.  
  86. /* ----- Get edit text field in a dialog ------------------------------- */
  87.  
  88. void GetEText(dPtr, item, str)
  89. register DialogPtr dPtr;
  90. register short item;
  91. register unsigned char *str;
  92. {
  93.     short theType;
  94.     Handle theItem;
  95.     Rect theBox;
  96.  
  97.     GetDItem(dPtr, item, &theType, &theItem, &theBox);
  98.     GetIText(theItem, str);
  99. }
  100.  
  101. /* ----- Set edit text field in a dialog ------------------------------- */
  102.  
  103. void SetEText(dPtr, item, str)
  104. register DialogPtr dPtr;
  105. register short item;
  106. register unsigned char *str;
  107. {
  108.     short theType;
  109.     Handle theItem;
  110.     Rect theBox;
  111.  
  112.     GetDItem(dPtr, item, &theType, &theItem, &theBox);
  113.     SetIText(theItem, str);
  114. }
  115.  
  116. /* ----- Get the value of a control ------------------------------------ */
  117.  
  118. short GetCheck(dPtr, ChkItem)
  119. register DialogPtr dPtr;
  120. register short ChkItem;
  121. {
  122.     short theType;
  123.     Handle theItem;
  124.     Rect theBox;
  125.  
  126.     GetDItem(dPtr, ChkItem, &theType, &theItem, &theBox);
  127.     return GetCtlValue(theItem);
  128. }
  129.  
  130. /* ----- Set the value of a control ------------------------------------ */
  131.  
  132. void SetCheck(dPtr, ChkItem, value)
  133. register DialogPtr dPtr;
  134. short ChkItem;
  135. short value;
  136. {
  137.     short theType;
  138.     Handle theItem;
  139.     Rect theBox;
  140.  
  141.     GetDItem(dPtr, ChkItem, &theType, &theItem, &theBox);
  142.     SetCtlValue(theItem, value);
  143. }
  144.  
  145. /* ----- Center dialog or alert template ------------------------------- */
  146.  
  147. void CenterDialog(templateType, templateID, bounds)
  148. register long templateType;    /* 'ALRT' or 'DLOG' */
  149. register short templateID;
  150. register Rect *bounds;
  151. {
  152.     register Rect *p;
  153.     register Rect **h;    /* Templates start with boundsRect */
  154.     register short width, height;
  155.     
  156.     if (!(h = (Rect **)GetResource(templateType, templateID)))
  157.         return;
  158.     p = *h;
  159.     width = p->right - p->left;
  160.     height = p->bottom - p->top;
  161.     p->bottom = bounds->bottom -
  162.         ((bounds->bottom - bounds->top - height) >> 1);
  163.     p->top = p->bottom - height;
  164.     p->left = bounds->left +
  165.         ((bounds->right - bounds->left - width) >> 1);
  166.     p->right = p->left + width;
  167. }
  168.  
  169. /* ----- Calculate where to put a centered dialog box ------------------ */
  170.  
  171. void GetDlogOrigin(dlogID, where, bounds)
  172. register short dlogID;
  173. register Point *where;
  174. register Rect *bounds;
  175. {
  176.     register Rect *p;
  177.     register Rect **h;    /* Templates start with boundsRect */
  178.  
  179.     if (!(h = (Rect **)GetResource('DLOG', dlogID))) {
  180.         where->v = where->h = 80;
  181.         return;
  182.     }
  183.     p = *h;
  184.     where->h = bounds->left +
  185.         ((bounds->right - bounds->left - (p->right - p->left)) >> 1);
  186.     where->v = bounds->top +
  187.         ((bounds->bottom - bounds->top - (p->bottom - p->top)) >> 1);
  188. }
  189.  
  190. /* ----- Hilite control ----------------------------------------------- */
  191.  
  192. SetHilite(dPtr, ChkItem, Value)
  193. DialogPtr dPtr;
  194. short ChkItem;
  195. short Value;
  196. {
  197.     short theType;
  198.     Handle theItem;
  199.     Rect theBox;
  200.  
  201.     GetDItem(dPtr, ChkItem, &theType, &theItem, &theBox);
  202.     HiliteControl(theItem, Value);
  203. }
  204.  
  205. /* ----- Error message ------------------------------------------------- */
  206.  
  207. void Message(n, code)
  208. register short n;
  209. register short code;
  210. {
  211.     register StringHandle h;
  212.     register DialogPtr dialogP;
  213.     short item;
  214.     unsigned char number[10];
  215.  
  216.     InitCursor();
  217.     SysBeep(1);
  218.     if (h = GetString(n)) {
  219.         HLock(h);
  220.         if (code)
  221.             NumToString((long)code, number);
  222.         else
  223.             number[0] = 0;
  224.         ParamText(*h, number, EmptyStr, EmptyStr);
  225.         CenterDialog('DLOG', rMessage, &Bounds);
  226.         if (dialogP = GetNewDialog(rMessage, 0L, -1L)) {
  227.             OutLine(dialogP, 1, patCopy);
  228.             ModalDialog(0L, &item);
  229.             DisposDialog(dialogP);
  230.         }
  231.         HUnlock(h);
  232.     }
  233. }
  234.